<template>
	<div class="app-container">
		<el-button class="top-left" type="danger" @click="backRoom">返回</el-button>
		<!-- <div class="header">
			<h2 class="title">预定会议</h2>
		</div> -->
		<!-- <el-row  :gutter="10">
			<el-form ref="meetingInfo" :inline="true" :model="form"   label-width="80px"   >
				<el-form-item label="会议主题">
				<el-input v-model="meetingInfo.meetingName" style="width: 50vh;"></el-input>
				</el-form-item>
				<el-form-item label="会议时间">
					<el-date-picker type="datetime" placeholder="选择日期"  value-format="yyyy-MM-dd HH:mm:ss"  v-model="meetingInfo.beginTime"></el-date-picker>
				</el-form-item>
				<el-button type="primary" @click="handleSendRequest" >创建会议</el-button>
				<el-button type="danger" @click="backRoom">返回</el-button>
			</el-form>
		</el-row> -->
		<div class="index-panel" :class="treeArr && treeArr.length <=5?'one-panel':''" v-if="deptType">
			<div v-for="(item,key) in treeArr" :key="key"
				:class="treeArr.length<=5?'panel-one':treeArr.length<=10?'panel-two':treeArr.length<=15?'panel-three':''"
				@click="changeDeptId(item.id,item)">{{item.label}}</div>
		</div>
		<div v-else class="popup-panel">
			<div>
			<label>{{title}}</label>
			<el-row :gutter="50">
				<!-- <el-col :span="5">
					<el-form ref="form" :model="form" label-width="80px" label-position="top">
						<el-form-item label="参会人员选择">
							<el-tree :data="treeData" show-checkbox :check-strictly="true" default-expand-all
								node-key="id" :default-expanded-keys="[2, 3]" :default-checked-keys="[5]"
								:props="defaultProps" @check="treeCheck" ref="tree">
							</el-tree>
						</el-form-item>
					</el-form>
				</el-col> -->
				<el-col :span="24" style="margin:30px 0pt;">
					<el-form ref="meetingInfo" :inline="true" :model="form" label-width="120px" label-position="top">
						<el-form-item label="会议主题" style="width: 48%;">
							<el-input v-model="meetingInfo.meetingName" placeholder="请输入会议主题" size="medium"></el-input>
						</el-form-item>
						<el-form-item label="会议时间" style="width: 50%;">
							<el-date-picker type="datetime" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss"
								v-model="meetingInfo.beginTime" style="width: 100%;" size="medium"></el-date-picker>
						</el-form-item>
						<!-- <el-button type="primary" @click="handleSendRequest">创建会议</el-button> -->
					</el-form>
				</el-col>
				<el-col :span="24">
					<label for="">选择参会人员</label>
					<el-table ref="multipleTable" :data="userList" @selection-change="handleSelectionChange" @select="UserSelect" @select-all="UserSelectAll"
						max-height="700" :row-key="getRowKey">
						<el-table-column type="selection" width="50" align="center"  :reserve-selection="true"/>
						<el-table-column label="手机号码" align="center" key="phonenumber" prop="phonenumber"
							:show-overflow-tooltip="true" />
						<el-table-column label="用户昵称" align="center" key="nickName" prop="nickName"
							:show-overflow-tooltip="true" />
						<el-table-column label="部门" align="center" key="deptName" prop="dept.deptName"
							:show-overflow-tooltip="true" />
						<el-table-column label="备注" align="center" key="remark" prop="remark" />
					</el-table>
					<pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
						:limit.sync="queryParams.pageSize" @pagination="getUserList" style="margin: 0 auto;" />
					</el-form>
				</el-col>

			</el-row>
			</div>
			<div style="display: flex;justify-content: center;">
				<el-button type="success" @click="dxqf">短信群发</el-button>
				<!-- <el-button type="primary" @click="cjhy">创建会议</el-button> -->
				<el-button type="primary" @click="handleSendRequest">创建会议</el-button>
				<el-button type="danger" @click="(deptType = true,selected_user_nums = [])">重新选择</el-button>
			</div>
		</div>
		<el-dialog title="短信群发" :visible.sync="dxVisible" width="70%" :before-close="handleClose">
			<el-form ref="smsInfo" :inline="true" :model="form" label-width="120px">
				<el-form-item label="短信内容">
					<el-input type="textarea" v-model="smsInfo.smsContent" style="width:700px" maxlength="500"
						show-word-limit></el-input>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button type="danger" @click="(dxVisible = false)">取 消</el-button>
				<el-button type="primary" @click="addDx">确 定</el-button>
			</span>
		</el-dialog>
		<el-dialog title="创建会议" :visible.sync="hyVisible" width="70%" :before-close="handleClose">
			<el-form ref="meetingInfo" :inline="true" :model="form" label-width="120px">
				<el-form-item label="会议主题">
					<el-input v-model="meetingInfo.meetingName" style="width: 50vh;"></el-input>
				</el-form-item>
				<el-form-item label="会议时间">
					<el-date-picker type="datetime" placeholder="选择日期" value-format="yyyy-MM-dd HH:mm:ss"
						v-model="meetingInfo.beginTime"></el-date-picker>
				</el-form-item>
				<!-- <el-button type="primary" @click="handleSendRequest">创建会议</el-button> -->
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button type="danger" @click="(hyVisible = false)">返回</el-button>
				<el-button type="primary" @click="handleSendRequest">创建会议</el-button>
			</span>
		</el-dialog>
	</div>
</template>

<script>
	import {
		mapGetters
	} from "vuex";
	import {
		treeselect
	} from "@/api/system/dept";
	import {
		listUser
	} from "@/api/system/user";
	import {
		addMeeting
	} from "@/api/system/meeting";
	import {
		addSendsms
	} from "@/api/system/sendsms";
	export default {
		data() {
			return {
				deptType: true, //选择部门
				smsInfo: {
					status: 0,
					masterId: 0,
					smsContent: "",
					beginTime: "",
					receiveMembers: "",
				},
				dxVisible: false, //发短信的弹出层
				hyVisible: false, //创建会议的弹出层
				form: {},
				info: {
					realName: "张三",
					name: "未命名",
					time: "2022-05-03 10:00:00 - 13:00:00",
					id: "123456789",
				},
				treeData: [],
				treeArr: [],
				defaultProps: {
					children: "children",
					label: "label",
				},
				currentCheckData: {}, // 当前选中的值
				userList: [],
				total: 0,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 50,
					userName: undefined,
					phonenumber: undefined,
					status: undefined,
					deptId: undefined,
				},
				selected_user_nums: [],
				meetingInfo: {
					status: 0,
					masterId: 0,
					meetingName: "",
					beginTime: "",
					meetingRoomId: "",
					meetingMembers: "",
				},
				deptId: -1,
				backup: null,
				title:null,
			};
		},
		computed: {
			...mapGetters(["userId"]),
			...mapGetters(["userObj"]),
		},
		created() {
			this.loadTreedata();
			let deptId = this.$route.query.dept;
			this.queryParams.deptId = deptId
			this.backup = this.$route.query.backup
			this.deptId = deptId
			this.getUserList();
			console.log(this.$route.query.name)
			if(this.$route.query.name){
				this.title = this.$route.query.name
			}
		},
		methods: {
			getRowKey(row){
				return row.phonenumber
			},
			toggleSelection() {
				for (let b = 0; b < this.userList.length; b++) {
					for (let i = 0; i < this.selected_user_nums.length; i++) {
						if (this.userList[b].phonenumber == this.selected_user_nums[i]) {
							console.log('选中')
							this.$refs.multipleTable.toggleRowSelection(this.userList[b]);
						}
					}
				}
			},
			handleClose() {
				this.dxVisible = false;
				this.hyVisible = false;
				this.smsInfo.smsContent = ""
				this.meetingInfo = {
					status: 0,
					masterId: 0,
					meetingName: "",
					beginTime: "",
					meetingRoomId: "",
					meetingMembers: "",
				}
			},
			// 发送手机短信
			addDx() {
				if (this.selected_user_nums.length == 0) {
					this.$message({
						message: "没有选择人员",
						type: "warning",
					});
					return;
				}
				if (this.smsInfo.smsContent == "") {
					this.$message({
						message: "请填写发送内容",
						type: "warning",
					});
					return;
				}
				this.smsInfo.masterId = this.userId;
				this.smsInfo.receiveMembers = this.selected_user_nums.toString();
				addSendsms(this.smsInfo).then((response) => {
					console.log(response);
					this.$message({
						message: "短信已发送",
						type: "success",
					});
					// this.backRoom();
					this.dxVisible = false
				});
			},
			dxqf() {
				this.dxVisible = true;
				// this.$router.push({
				// 	path: "/sendsms",
				// 	query: "",
				// });
			},
			cjhy() {
				//弹出层
				this.hyVisible = true;
			},
			backRoom() {
				// this.$router.push({
				// 	path: "/dept",
				// 	query: {
				// 		dept: this.backup
				// 	}
				// });
				this.$router.back()
			},
			UserSelect(selection){
				console.log(selection)
				this.selected_user_nums = selection.map((item) => item.phonenumber);
			},
			UserSelectAll(selection){
				console.log(selection)
				this.selected_user_nums = selection.map((item) => item.phonenumber);
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				// console.log(selection)
				// if (selection.length > 0 && selection[0] != 0) {
				// 	this.selected_user_nums = selection.map((item) => item.phonenumber);
				// }
				// console.log(this.selected_user_nums);
			},
			handleCancel() {
				this.centerDialogVisible = false;
			},
			//创建会议，后台发送手机短信会议邀请
			handleSendRequest() {
				if (this.selected_user_nums.length == 0) {
					this.$message({
						message: "没有选择人员",
						type: "warning",
					});
					return;
				}
				if (
					this.meetingInfo.meetingName == "" ||
					this.meetingInfo.beginTime == ""
				) {
					this.$message({
						message: "请填写会议主题和时间",
						type: "warning",
					});
					return;
				}
				this.meetingInfo.masterId = this.userId;
				this.selected_user_nums.push(this.userObj.phonenumber); //把自己加入会议人员名单
				this.meetingInfo.meetingMembers = this.selected_user_nums.toString();
				this.meetingInfo.meetingRoomId = this.randomRoomNum();
				addMeeting(this.meetingInfo).then((response) => {
					console.log(response);
					this.$message({
						message: "已通过短信发送会议邀请",
						type: "success",
					});
					this.backRoom();
				});
			},
			randomRoomNum() {
				return Math.floor(Date.now() / 1000)
			},
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getUserList();
			},
			/** 查询用户列表 */
			getUserList() {
				if (this.queryParams.deptId) {
					//
				} else {
					this.queryParams.deptId = this.deptId
				}
				listUser(this.queryParams).then((response) => {
					console.log(this.userList)
					this.userList = response.rows;
					this.total = response.total;
					this.toggleSelection();
				});
			},
			changeDeptId(deptId,item) {
				
				console.log(item)
				this.title = item.label;
				this.deptType = false;
				this.queryParams.deptId = deptId
				this.getUserList();
			},
			// treeCheck(nodeObj, selectObj) {
			treeCheck(data) {

				if (this.currentCheckData.id === data.id) {
					this.currentCheckData = {};
					this.$refs.tree.setCheckedKeys([]);
				} else {
					this.currentCheckData = data;
					this.$refs.tree.setCheckedKeys([data.id]);
				}
				this.queryParams.deptId = this.currentCheckData.id;
				this.getUserList();
			},
			loadTreedata() {
				let deptId = this.$route.query.dept;
				if (deptId) {} else {
					deptId = ""
				}
				treeselect(deptId).then((response) => {
					//根据response.data[0].children判断是否有下级部门，没有展示人员列表；
					if (response.data && response.data.length > 0 && response.data[0].children) {
						this.treeData = response.data;
						this.treeArr = response.data[0].children;
					} else {
						this.deptType = false
					}

				});
			},
		},
	};
</script>
<style scoped>
	::-webkit-scrollbar {
		/*高宽分别对应横竖滚动条的尺寸*/
		width: 10px;
		background-color: rgba(255, 255, 255, 0.1);
	}

	.app-container {
		/* background-color: #ffffff;
		height: calc(100% - 200px);
		overflow-x: hidden; */
		/* border-radius: 20px; */
	}

	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.video-box {
		overflow-x: hidden;
		height: calc(100vh - 230px);
	}

	.video-box div {
		float: left;
	}

	.video-box .all {
		border: 1px solid #ccc;
		width: calc(310px - 20px);
		margin: 10px;
		padding: 20px;
		border-radius: 10px;
	}

	.btn-box {
		position: fixed;
		bottom: 20px;
		width: 100%;
		display: flex;
		justify-content: center;
	}

	.index-panel {
		margin: 100px;
		height: calc(100vh - 260px);
		overflow-x: hidden;
	}

	.index-panel>div {
		display: flex;
		align-items: center;
		width: calc(20% - 40px);
		float: left;
		margin: 20px;
		height: 120px;
		text-align: center;
		background-image: linear-gradient(to bottom, #ffda81, #fff9c6);
		padding: 30px;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 30px;
		font-size: 26px;
		font-weight: bold;
		color: #f20000;
	}

	.popup-panel {
		margin: 100px;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
		height: calc(100vh - 260px);
		background-color: #ffffff;
		overflow-x: hidden;
		border-radius: 20px;
		padding: 30px
	}

	.top-left {
		position: fixed;
		left: 30px;
		top: 30px;
	}
</style>
